﻿using System;
using EnvDTE;
using EnvDTE80;

namespace VsTortoiseSVN.Commands
{

    public abstract class ActiveDocumentBase : CommandBase
    {
        /// <summary>
        /// Gets the full file path of the active document.
        /// </summary>
        public string FullName
        {
            get
            {
                return Application.ActiveDocument.LongFullName();
            }
        }

        /// <summary>
        /// Gets active line of cursor in active document.
        /// </summary>
        public int ActiveLine
        {
            get
            {
                if (HasActivePoint())
                    return (Application.ActiveDocument.Selection as TextSelection).ActivePoint.Line;

                return 0;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public ActiveDocumentBase(DTE2 app, string name, string caption, string tooltip, int bitmapId)
            : base(app, name, caption, tooltip, bitmapId)
        {
        }


        /// <summary>
        /// Gets whether the active document has an active point, where
        /// an active point refers to the current position of the cursor.
        /// </summary>
        /// <returns></returns>
        protected bool HasActivePoint()
        {
            // paranoia checks to avoid null accesses
            if (Application == null || Application.ActiveDocument == null || Application.ActiveDocument.Selection == null)
                return false;

            TextSelection selection = Application.ActiveDocument.Selection as TextSelection;
            return selection.ActivePoint != null;
        }
    }
}
